define(function(require,exports,module){
	function getText(pk,datas,keyField,valueField){
		var length = datas.length;
		for(var i=0;i<length;i++){
			if(datas[i][keyField] == pk){
				return datas[i][valueField];
			}
		}
	}
	
	
	module.exports = {
		procSubFormData: function(data,productTypes
				,productBatches,units,eRules,eScopes,bRules,bScopes,cRules,cScopes){
			var ret = {};
			//产品类型
			ret.productType = {
				pkProductType: data.productType,
				name: getText(data.productType,productTypes,"pkProductType","name")
			};
			//产品批次
			ret.productBatch = data.productBatch ? {
				pkProductBatch: data.productBatch,
				name: getText(data.productBatch,productBatches,"pkProductBatch","name")
			} : null;
			ret.price = data.price;
			ret.validity = data.validity;
			ret.liveDays = data.liveDays;
			ret.unit = {
				key: data.unit,
				value: getText(data.unit,units,"key","value")
			}
			
			var settlementsRules = [];
			if(data.settlementsRules_Expense){
				settlementsRules.push({
					pkSettlementsRule: data.settlementsRules_Expense,
					name: getText(data.settlementsRules_Expense,eRules,"pkSettlementsRule","name"),
					type: {
						key: "Expense",
						value: "费用抵扣"
					}
				});
			}
			if(data.settlementsRules_BedSubsidy){
				settlementsRules.push({
					pkSettlementsRule: data.settlementsRules_BedSubsidy,
					name: getText(data.settlementsRules_BedSubsidy,bRules,"pkSettlementsRule","name"),
					type:{
						key: "BedSubsidy",
						value: "床位补贴"
					}
				});
			}
			if(data.settlementsRules_Coupon){
				settlementsRules.push({
					pkSettlementsRule: data.settlementsRules_Coupon,
					name: getText(data.settlementsRules_Coupon,cRules,"pkSettlementsRule","name"),
					type: {
						key: "Coupon",
						value: "福利券发放"
					}
				});
			}
			ret.settlementsRules = settlementsRules;
			
			var ruleScope = [];
			for(var i=0;i<data.ruleScope_Expense.length;i++){
				ruleScope.push({
					pkScope: data.ruleScope_Expense[i],
					name: getText(data.ruleScope_Expense[i],eScopes,"pkScope","name"),
					ruleType: {
						key: "Expense",
						value: "费用抵扣"
					}
				});
			}
			for(var i=0;i<data.ruleScope_BedSubsidy.length;i++){
				ruleScope.push({
					pkScope: data.ruleScope_BedSubsidy[i],
					name: getText(data.ruleScope_BedSubsidy[i],bScopes,"pkScope","name"),
					ruleType:{
						key: "BedSubsidy",
						value: "床位补贴"
					}
				});
			}
			for(var i=0;i<data.ruleScope_Coupon.length;i++){
				ruleScope.push({
					pkScope: data.ruleScope_Coupon[i],
					name: getText(data.ruleScope_Coupon[i],cScopes,"pkScope","name"),
					ruleType: {
						key: "Coupon",
						value: "福利券发放"
					}
				});
			}
			ret.ruleScopes = ruleScope;
			ret.seal = (data.seal == "true" ? true: false);
			return ret;
		},
		groupRulesOrScopes: function(data){
			var length = data.length;
			var ret = {
				expense: [],
				bedSubsidy: [],
				coupon: []
			};
			for(var i=0;i<length;i++){
				var type = data[i].type || data[i].ruleType;
				if(type.key == "Expense"){
					ret.expense.push(data[i]);
				}else if(type.key == "BedSubsidy"){
					ret.bedSubsidy.push(data[i]);
				}else if(type.key == "Coupon"){
					ret.coupon.push(data[i]);
				}
			}
			return ret;
		},
		getGridFetchProperties: function(){
			return "*,productRule.product.pkProduct," +
					"productRule.product.name," +
					"productType.pkProductType," +
					"productType.name," +
					"productBatch.pkProductBatch," +
					"productBatch.name," +
					"productRule.communityDatas.pkCommunityData," +
					"productRule.*," +
					"settlementsRules.*," +
					"ruleScopes.*";
		}
	}
});